package com.cooldatasoft.domain.daoImpl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;

import com.cooldatasoft.domain.dao.UserDao;
import com.cooldatasoft.domain.model.User;

public class UserDaoImpl extends GenericDaoImpl<User, Long> implements UserDao {

	@SuppressWarnings("unchecked")
	@Override
	public User getUserByEmail(String email) {
		Criteria reservationCrit = getSession().createCriteria(User.class);
		reservationCrit.add(Restrictions.eq("email", email));
		
		List<User> res = reservationCrit.list();
		return (res.size() > 0) ? res.get(0) : null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public User getUserById(Long id) {
		Criteria reservationCrit = getSession().createCriteria(User.class);
		reservationCrit.add(Restrictions.eq("idUser", id));
		
		List<User> res = reservationCrit.list();
		return (res.size() > 0) ? res.get(0) : null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public User getUserBySurname(String surname) {
		Criteria reservationCrit = getSession().createCriteria(User.class);
		reservationCrit.add(Restrictions.eq("surname", surname));
		
		List<User> res = reservationCrit.list();
		return (res.size() > 0) ? res.get(0) : null;
	}

	@Override
	public void updateUser(User user) {
		getSession().update(user);
	}

	@SuppressWarnings("unchecked")
	@Override
	public User getUserByPassword(String openId) {
		Criteria reservationCrit = getSession().createCriteria(User.class);
		reservationCrit.add(Restrictions.eq("password", openId));
		
		List<User> res = reservationCrit.list();
		return (res.size() > 0) ? res.get(0) : null;
	}
}
